package com.zhku.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhku.base.exception.TaoTaoException;
import com.zhku.base.model.PageParams;
import com.zhku.base.model.PageResult;
import com.zhku.user.mapper.TtUserAddressMapper;
import com.zhku.user.model.dto.AddressDto;
import com.zhku.user.model.dto.QueryAddressParamsDto;
import com.zhku.user.model.po.TtUserAddress;
import com.zhku.user.service.TtUserAddressService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class TtUserAddressServiceImpl extends ServiceImpl<TtUserAddressMapper, TtUserAddress> implements TtUserAddressService {

    @Autowired
    TtUserAddressMapper ttUserAddressMapper;

    @Transactional
    @Override
    public TtUserAddress createAddress(AddressDto addressDto) {
        TtUserAddress ttUserAddress = new TtUserAddress();
        BeanUtils.copyProperties(addressDto,ttUserAddress);
        String id = UUID.randomUUID().toString();
        ttUserAddress.setId(id);
        ttUserAddress.setCreatetime(LocalDateTime.now());
        ttUserAddress.setUpdatetime(LocalDateTime.now());
        int insert = ttUserAddressMapper.insert(ttUserAddress);
        if (insert <= 0){
            throw new TaoTaoException("创建新收货地址失败");
        }
        return ttUserAddress;
    }

    @Transactional
    @Override
    public void deleteAddress(String id) {
        if (StringUtils.isEmpty(id)){
            throw new TaoTaoException("错误：传入空的收货id");
        }
        int i = ttUserAddressMapper.deleteById(id);
        if (i <= 0){
            throw new TaoTaoException("删除收货地址失败");
        }
    }

    @Transactional
    @Override
    public TtUserAddress updateAddress(TtUserAddress ttUserAddress) {
        String id = ttUserAddress.getId();
        TtUserAddress ttUserAddressOld = ttUserAddressMapper.selectById(id);
        if (ttUserAddressOld == null) {
            throw new TaoTaoException("当前收货地址不存在");
        }
        ttUserAddress.setUpdatetime(LocalDateTime.now());
        int i = ttUserAddressMapper.updateById(ttUserAddress);
        if (i <= 0){
            throw new TaoTaoException("修改收货地址失败");
        }
        return ttUserAddress;
    }

    @Override
    public TtUserAddress getAddress(String id) {
        if (StringUtils.isEmpty(id)){
            throw new TaoTaoException("传入空的收货地址id");
        }
        TtUserAddress ttUserAddress = ttUserAddressMapper.selectById(id);
        if (ttUserAddress == null) {
            throw new TaoTaoException("收货地址不存在");
        }
        return ttUserAddress;
    }

    @Override
    public PageResult<TtUserAddress> queryAddressList(PageParams pageParams, QueryAddressParamsDto queryAddressParamsDto) {
        //拼装查询条件
        LambdaQueryWrapper<TtUserAddress> queryWrapper = new LambdaQueryWrapper<>();
        //根据用户id进行查询
        queryWrapper.like(StringUtils.isNotEmpty(queryAddressParamsDto.getUserId()),
                TtUserAddress::getUserId,queryAddressParamsDto.getUserId());
        //创建page分页参数对象，参数：当前页码数，每页记录数
        Page<TtUserAddress> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
        Page<TtUserAddress> ttUserAddressPage = ttUserAddressMapper.selectPage(page, queryWrapper);
        List<TtUserAddress> items = ttUserAddressPage.getRecords();
        long counts = ttUserAddressPage.getTotal();
        PageResult<TtUserAddress> ttUserAddressPageResult = new PageResult<>(items,
                counts,pageParams.getPageNo(),pageParams.getPageSize());
        return ttUserAddressPageResult;
    }
}
